<?php
namespace App\Services;


use Illuminate\Support\Facades\DB;

/**
 *
 * 分成比例服务层
 *
 * @author shengl@maqu.im
 *
 */
class CommissionService extends BaseService {

    /**
     *  订单完成收益分成
     *
     * @param $account_id 账户ID
     * @param $commission_type 提成比例类型
     * @param $amount 金额
     * @param bool $withTrans 是否启用事务
     * @return array
     * @throws \Exception
     */
    public function getCommission($seller_id,$amount,$commission_type){

       $franchisee_id = DB::table('member')->where('user_id',$seller_id)->value('parent_id');//加盟商id

       if($commission_type == 'goods'){

           $franchisee_commission_goods = DB::table('member_agent')->where('user_id',$franchisee_id)->value('commission_goods');//加盟商商品比例
           $partner_commission = DB::table('member_partner')->where('user_id',$seller_id)->value('commission_goods');//合作者收益比例
           $partner_money = round(($amount * $partner_commission/100),2);//乡村合作者收益
           $franchisee_money  = round(($amount * ($franchisee_commission_goods - $partner_commission)/100),2);//加盟商收益
           $admin_money = $amount - $partner_money - $franchisee_money;//平台收益

       }elseif($commission_type == 'tour'){

           $franchisee_commission_goods = DB::table('member_agent')->where('user_id',$franchisee_id)->value('commission');//加盟商商品比例
           $partner_commission = DB::table('member_partner')->where('user_id',$seller_id)->value('commission');//合作者收益比例
           $partner_money = round(($amount * $partner_commission/100),2);//乡村合作者收益
           $franchisee_money  = round(($amount * ($franchisee_commission_goods - $partner_commission)/100),2);//加盟商收益
           $admin_money = $amount - $partner_money - $franchisee_money;//平台收益

       }elseif($commission_type == 'landowners'){
           $franchisee_commission_goods = DB::table('member_agent')->where('user_id',$franchisee_id)->value('commission_landowner');//加盟商商品比例
           $partner_commission = DB::table('member_partner')->where('user_id',$seller_id)->value('commission_landowner');//合作者收益比例
           $partner_money = round(($amount * $partner_commission/100),2);//乡村合作者收益
           $franchisee_money  = round(($amount * ($franchisee_commission_goods - $partner_commission)/100),2);//加盟商收益
           $admin_money = $amount - $partner_money - $franchisee_money;//平台收益
       }
        $data['franchisee_commission_goods'] = $franchisee_commission_goods;
        $data['partner_commission'] = $partner_commission;
        $data['franchisee_id'] = $franchisee_id;
        $data['admin_money'] = $admin_money;
        $data['franchisee_money'] = $franchisee_money;
        $data['partner_money'] = $partner_money;

        return $data;

    }




}
